package vector

import (
	"context"

	"github.com/qdrant/go-client/qdrant"
	"go-file-perception-model/internal/model"
)

// QdrantDB Qdrant向量数据库协调器
type QdrantDB struct {
	client           *QdrantClient
	collectionMgr    *QdrantCollectionManager
	pointsMgr        *QdrantPointsManager
	searchMgr        *QdrantSearchManager
}

// NewQdrantDB 创建Qdrant向量数据库实例
func NewQdrantDB(host string, port int) (*QdrantDB, error) {
	return NewQdrantDBWithAPIKey(host, port, "")
}

// NewQdrantDBWithAPIKey 创建带有API密钥的Qdrant向量数据库实例
func NewQdrantDBWithAPIKey(host string, port int, apiKey string) (*QdrantDB, error) {
	// 创建Qdrant客户端
	client, err := NewQdrantClientWithAPIKey(host, port, apiKey)
	if err != nil {
		return nil, err
	}

	// 创建各个管理器
	collectionMgr := NewQdrantCollectionManager(client)
	pointsMgr := NewQdrantPointsManager(client)
	searchMgr := NewQdrantSearchManager(client)

	return &QdrantDB{
		client:        client,
		collectionMgr: collectionMgr,
		pointsMgr:     pointsMgr,
		searchMgr:     searchMgr,
	}, nil
}

// CreateCollection 创建集合
func (db *QdrantDB) CreateCollection(ctx context.Context, collectionName string, vectorSize int) error {
	return db.collectionMgr.CreateCollection(ctx, collectionName, vectorSize)
}

// CollectionExists 检查集合是否存在
func (db *QdrantDB) CollectionExists(ctx context.Context, collectionName string) (bool, error) {
	return db.collectionMgr.CollectionExists(ctx, collectionName)
}

// InsertPoints 插入向量点
func (db *QdrantDB) InsertPoints(ctx context.Context, points []*model.FileChunk) error {
	return db.pointsMgr.InsertPoints(ctx, points)
}

// SearchPoints 搜索向量点
func (db *QdrantDB) SearchPoints(ctx context.Context, vector []float32, limit int, threshold float32, filter *Filter) ([]*model.SearchResult, error) {
	return db.searchMgr.SearchPoints(ctx, vector, limit, threshold, filter)
}

// DeletePoints 删除向量点
func (db *QdrantDB) DeletePoints(ctx context.Context, ids []string) error {
	return db.pointsMgr.DeletePoints(ctx, ids)
}

// DeletePointsByFilter 根据过滤器删除向量点
func (db *QdrantDB) DeletePointsByFilter(ctx context.Context, filter *Filter) error {
	return db.pointsMgr.DeletePointsByFilter(ctx, filter)
}

// DeleteAllPoints 删除所有向量点
func (db *QdrantDB) DeleteAllPoints(ctx context.Context) error {
	return db.pointsMgr.DeleteAllPoints(ctx)
}

// CountPoints 计算集合中的点数量
func (db *QdrantDB) CountPoints(ctx context.Context, filter *Filter) (uint64, error) {
	return db.pointsMgr.CountPoints(ctx, filter)
}

// GetCollectionInfo 获取集合信息
func (db *QdrantDB) GetCollectionInfo(ctx context.Context, collectionName string) (*qdrant.CollectionInfo, error) {
	return db.collectionMgr.GetCollectionInfo(ctx, collectionName)
}

// SetCollection 设置当前使用的集合
func (db *QdrantDB) SetCollection(collectionName string) {
	db.client.SetCollection(collectionName)
}

// GetCollection 获取当前使用的集合名称
func (db *QdrantDB) GetCollection() string {
	return db.client.GetCollection()
}

// Close 关闭数据库连接
func (db *QdrantDB) Close() error {
	return db.client.Close()
}

// GetPointByID 根据点ID获取点的详细信息
func (db *QdrantDB) GetPointByID(ctx context.Context, id string) (*model.SearchResult, error) {
	return db.pointsMgr.GetPointByID(ctx, id)
}

// ListCollections 列出所有集合
func (db *QdrantDB) ListCollections(ctx context.Context) ([]string, error) {
	return db.collectionMgr.ListCollections(ctx)
}